<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../../../src/lib/jquery.min.js"></script>
    <link rel="stylesheet" href="less/toggle.css">

    <style>
        .demo{
            width:600px;
            height:500px;
            background-color:#dbdbdb;
            margin:0 auto;
        }
        .btn{
            display: inline-block;
            width: 100px;
            height:30px;
            border-radius: 5px;
            background-color: #1fb394;
            font-size: 14px;
            text-align: center;
            line-height: 30px;
            color:#fff;
        }
    </style>
</head>
<body>
<div class="demo">

    <div class="img-box">
        <img class="box-img" src="../src/img/01.jpg" alt="">
        <!--<div class="box-layer"><img src="img/timg.gif" alt=""></div>-->
    </div>

    <ul class="color-item">
        <li data-val="../src/img/01.jpg" class="item-list active"></li>
        <li data-val="https://img.alicdn.com/tps/i4/TB1fbhiawoQMeJjy0FnSuv8gFXa.jpg_490x490q100.jpg_.webp" class="item-list"></li>
        <li data-val="//pr.aopcdn.com//banner/1504170427917.jpg?ver=15041704" class="item-list"></li>
    </ul>
</div>
<script>





    //图片切换
    $("body").on("click", ".item-list", function(){
        changeMainPic($(this))
    })



    function changeMainPic(that) {
        var _this = that;
        var imgObj =  _this.parents('.demo').find(".img-box").find('img');
        _this.parents('.demo').find(".img-box").find('.box-layer').remove();

        //切换列表样式改变
        _this.addClass("active").siblings().removeClass("active");

        var mid = new Image();
            mid.onload = function(){
                _this.parents('.demo').find(".img-box").find('.box-layer').remove();
            };

            //mid.src = '';
            mid.src =  _this.attr('data-val');
        if(!mid.complete){
            _this.parents('.demo').find('.img-box').append('<div class="box-layer"><img src="../src/img/timg.gif" alt=""></div>');
        }
        imgObj.prop({
            "src":mid.src
        })
    }


</script>

</body>
</html>